<!-- <template>
  <div class="login">
    <img class="top-bg" alt="" src="../assets/login/top-bg.png" />
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item  class="btn">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <div class="right-wrap">
      <img alt="" src="../assets/login/bg.png" />
    </div>
    <div class="el-login-footer" @click="goJump">
      <span>陕ICP备2023021549号</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    goJump(){
      window.open('https://beian.miit.gov.cn/','_blank');
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", this.loginForm.password, { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  position: relative;
  background: rgba(255, 255, 255, 1);
}

.title {
  margin: 15% auto 10% auto;
  text-align: center;
  color: rgba(0, 0, 0, 1);
  font-size: 24px;
  font-weight: 600;
}

.login-form {
  background: #ffffff;
  min-width: 320px;
  min-height: 350px;
  padding:0 30px;
  z-index: 1;
  border-radius: 20px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 10px 40px  rgba(158, 195, 255, 0.51);
  width: 20%;
  height: 50%;
  position: absolute;
  left:12%;
  top:calc(50% - 25%);
  .btn{
    width: 100%;
    margin-top: 10% !important;
    .el-button--primary{
      background-color:rgba(25, 117, 226, 1) !important;
      border-color: rgba(25, 117, 226, 1) !important;
    }
  }
  @media (max-width: 1440px) {
    .btn { margin-top:4% !important;}
  }
  @media (max-width: 1024px) {
    .btn { margin-top: 0 !important;}
  }
  .el-form-item{
    margin-bottom:10% !important;
  }
  .el-input {
    height: 38px;
    background: rgba(240, 247, 255, 1);
    border: none !important;
    input {
      height: 38px;
      background: rgba(240, 247, 255, 1);
      border: none !important;
      border-radius: 5px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }



}
.right-wrap{
  width:68%;
  height:66%;
  position: absolute;
  bottom: 0;
  right: 0;
  img{
    width: 100%;
    height:100%;
    position: absolute;
    bottom:0;
    right: 0;
  }
}

.top-bg{
  position: absolute;
  top: 0;
  left: 0;
  height: 18%;
  width: 40%;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style> -->


<template>
  <div class="login">
    <!-- <img class="top-bg" alt="" src="../assets/login/top-bg.png" /> -->
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="padding: 13px;width: 100%;font-size: 16px;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!-- <div class="right-wrap">
      <img alt="" src="../assets/login/bg.png" />
    </div> -->
    <div class="el-login-footer" @click="goJump">
      <span>陕ICP备2023021549号-2</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    goJump() {
      window.open('https://beian.miit.gov.cn/', '_blank');
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", this.loginForm.password, { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url("../assets/login/login.jpg") no-repeat center;
  background-size: 100% 100%;
  position: relative;
}

.title {
  margin: 15% auto 10% auto;
  text-align: center;
  color: rgba(0, 0, 0, 1);
  font-size: 24px;
  font-weight: 600;
}

.login-form {
  background: #b1cfee !important;
  min-width: 320px;
  min-height: 350px;
  padding: 0 30px;
  z-index: 1;
  border-radius: 20px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 10px 40px rgba(158, 195, 255, 0.51);
  width: 26%;
  height: 50%;
  position: absolute;
  right: 10%;
  top: 50%;
  /* 使用百分比值 */
  transform: translateY(-50%);

  .btn {
    width: 100%;
    margin-top: 10% !important;

    .el-button--primary {
      background-color: rgba(25, 117, 226, 1) !important;
      border-color: rgba(25, 117, 226, 1) !important;
    }
  }

  .el-form-item {
    margin-bottom: 10% !important;
  }

  .el-input {
    height: 38px;
    background: rgba(240, 247, 255, 1);
    border: none !important;

    input {
      height: 38px;
      background: rgba(240, 247, 255, 1);
      border: none !important;
      border-radius: 5px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }



}

.right-wrap {
  width: 68%;
  height: 66%;
  position: absolute;
  bottom: 0;
  right: 0;

  img {
    width: 100%;
    height: 100%;
    position: absolute;
    bottom: 0;
    right: 0;
  }
}

.top-bg {
  position: absolute;
  top: 0;
  left: 0;
  height: 18%;
  width: 40%;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
